package com.wzh.oj.factory;

import com.wzh.oj.handler.MyUncaughtExceptionHandler;
import lombok.AllArgsConstructor;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.ThreadFactory;

/**
 * @author wzh
 * 使用装饰器模式对每个创建的线程进行一个增强的处理
 * 装饰器模式分为继承和实现2种方式
 */
@AllArgsConstructor
public class MyThreadFactory implements ThreadFactory {

    private ThreadFactory threadFactory;
    @Override
    public Thread newThread(@NotNull Runnable r) {
        Thread thread = threadFactory.newThread(r);
        thread.setUncaughtExceptionHandler(MyUncaughtExceptionHandler.getMyUncaughtExceptionHandler());
        return thread;
    }
}
